package com.cnbm.wcsbasic.protocol.netty.s7.s7connector.api.utils;

/**
 * @Desc: ""
 * @Author: caixiang
 * @DATE: 2021/12/16 9:08
 */

import com.cnbm.wcsbasic.protocol.netty.s7.s7connector.exception.S7ParseDataException;
import com.cnbm.wcsbasic.protocol.netty.s7.s7connector.utils.CommonFunctions;

import java.io.UnsupportedEncodingException;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.*;

public class ByteUtils {


    public static Boolean toBoolean(byte[] bytes){
        if(bytes.length ==0){
            return null;
        }
        //只要!=0,那就是大于等于1 ，其实读单个bool变量 ，读取到的byte[] 数组长度都是1.
        byte[] byteArrayAndInvert = toByteArrayAndInvert(bytes[0]);
        if(byteArrayAndInvert[0] == 1){
            return true;
        }else {
            return false;
        }
    }
    public static byte[] toByteArrayAndInvert(byte b){
        byte[] arr=new byte[8];
        int len=arr.length;
        for(int i=0;i<len;i++){
            arr[i]=(byte)((b>>(len-1-i))&0x1);
        }
        return invert(arr);
    }

    public static double bytes2Double(byte[] arr) {
        long value = 0;
        for (int i = 0; i < 8; i++) {
            value |= ((long) (arr[i] & 0xff)) << (8 * i);
        }

        return Double.longBitsToDouble(value);
    }

//    public static Boolean toBoolean(byte[] bytes){
//        if(bytes.length ==0){
//            return null;
//        }
//        if(bytes[0] == 1){
//            return true;
//        }else {
//            return false;
//        }
//    }

    public static Boolean toBoolean(byte bytes){
        if(bytes == 1){
            return true;
        }else {
            return false;
        }
    }

    public static String addDate(String timeParam, Long day) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); // 日期格式
        Date date = null;
        try {
            date = dateFormat.parse(timeParam); // 指定日期
        }catch (Exception e){
            throw new S7ParseDataException("ByteUtils.addDate error:", e);
        }

        long time = date.getTime(); // 得到指定日期的毫秒数
        day = day * 24 * 60 * 60 * 1000; // 要加上的天数转换成毫秒数
        time += day; // 相加得到新的毫秒数
        Date newDate = new Date(time);
        return dateFormat.format(newDate); // 将毫秒数转换成日期
    }


    public static byte[] invert(byte[] a){
        byte[] b = new byte[a.length];
        Stack<Byte> st = new Stack<Byte>();
        for(int i=0;i<a.length;i++){
            st.push(a[i]);
        }
        for(int i=0;i<a.length;i++){
            b[i] = st.pop();
        }
        return b;
    }


    /**
     * 1个字节byte[] 转成有符号的Integer
     * */
    public static Integer toInt(byte bytes) {
        return Integer.valueOf(Byte.toString(bytes));
    }
    /**
     * 1个字节byte[] 转成无符号的Integer
     * */
    public static Integer toUInt(byte bytes) {
        int i = Byte.toUnsignedInt(bytes);
        return Integer.valueOf(i);
    }


    /**
     * 2个字节byte[] 转成有符号的Integer
     * 默认大端
     * */
    public static Integer toInt(byte byte1,byte byte2) {
        byte[] bytes = new byte[2];
        bytes[0] = byte1;
        bytes[1] = byte2;

        return Integer.valueOf(Short.toString(ByteUtil.bytesToShort(bytes, ByteOrder.BIG_ENDIAN)));
    }
    /**
     * 2个字节byte[] 转成无符号的Integer
     * 默认大端
     * */
    public static Integer toUInt(byte byte1,byte byte2) {
        byte[] bytes = new byte[2];
        bytes[0] = byte1;
        bytes[1] = byte2;
        short i = ByteUtil.bytesToShort(bytes, ByteOrder.BIG_ENDIAN);
        return Short.toUnsignedInt(i);
    }


    /**
     * 4个字节byte[] 转成有符号的Integer
     * 默认大端
     * */
    public static Integer toInt(byte byte1,byte byte2,byte byte3,byte byte4) {
        byte[] bytes = new byte[4];
        bytes[0] = byte1;
        bytes[1] = byte2;
        bytes[2] = byte3;
        bytes[3] = byte4;
        return ByteUtil.bytesToInt(bytes, ByteOrder.BIG_ENDIAN);
    }
    /**
     * 4个字节byte[] 转成无符号的Long（因为如果首位为1 Integer就不满足长度了）
     * 默认大端
     * */
    public static Long toUInt(byte byte1,byte byte2,byte byte3,byte byte4) {
        byte[] bytes = new byte[8];
        bytes[0] = 0;
        bytes[1] = 0;
        bytes[2] = 0;
        bytes[3] = 0;
        bytes[4] = byte1;
        bytes[5] = byte2;
        bytes[6] = byte3;
        bytes[7] = byte4;
        long l = ByteUtil.bytesToLong(bytes, ByteOrder.BIG_ENDIAN);
        return l;
    }

    /**
     *
     * 4个字节byte[] 转成有符号的Double
     * 默认大端
     * */
    public static Float forReal(byte[] bytes) {
        return Float.intBitsToFloat(toInt(bytes[0],bytes[1],bytes[2],bytes[3]));
    }
    /**
     *
     * 8个字节byte[] 转成有符号的Double
     * 默认大端
     * */
    public static Double forLReal(byte[] bytes) {
        return ByteUtil.bytesToDouble(bytes, ByteOrder.BIG_ENDIAN);
    }

    /**
     *
     * 8个字节byte[] 转成有符号的Double
     * 默认大端
     * */
    public static Double lrealbytesToDouble(byte[] bytes) {
        return ByteUtil.bytesToDouble(bytes, ByteOrder.BIG_ENDIAN);
    }
    public static byte[] lrealToBytes(double doubleValue) {
        return ByteUtil.doubleToBytes(doubleValue, ByteOrder.BIG_ENDIAN);
        //return double2Bytes(doubleValue);
    }

//    public static String toChar(byte[] b) throws UnsupportedEncodingException {
//        String ascii = new String(b, Charset.forName("UTF-8"));
//        return ascii;
//    }
    public static Character toChar(byte[] b)  {
        if(b.length==1){
            return toChar(b[0]);
        }
        return byteToChar(b);
    }
    public static Character toChar(byte b){
        return byteToChar(b);
    }
//    public static String toChar(byte b) throws UnsupportedEncodingException {
//        byte[] bs = new byte[1];
//        bs[0] = b;
//        String ascii = new String(bs, "ascii");
//        return ascii;
//    }

    /**
     * return null 代表返回传入参数不正确str 取的length太小
     * */
    public static String toStr(byte[] b) {
        Integer length = Byte.toUnsignedInt(b[1]);
        if(length>(b.length-2)){
            return null;
        }
        byte[] content = new byte[b.length-2];
        for(int i=0;i<length;i++){
            content[i] = b[i+2];
        }
        String ascii = new String(content, StandardCharsets.UTF_8);
       // String s = new String(content);
        return ascii;
    }
    public static String[] toStrArray(byte[] b,Integer length,Integer strSize) {
        String[] res = new String[length];
        strSize+=2;
        for(int i=0;i<length;i++){
            byte[] one = new byte[strSize];
            System.arraycopy(b,i*strSize,one,0,strSize);
            res[i] = toStr(one);
        }
        return res;
    }

    /**
     * 将byte转换为一个长度为8的boolean数组(每bit代表一个boolean值)
     *
     * @param b byte, return(将数组翻转过来)
     * @return boolean数组
     */

    public static boolean[] getBooleanArray(byte b,boolean returns) {

        boolean[] array = new boolean[8];

        for (int i = 7; i >= 0; i--) { //对于byte的每bit进行判定

            array[i] = (b & 1) == 1; //判定byte的最后一位是否为1，若为1，则是true；否则是false

            b = (byte) (b >> 1); //将byte右移一位

        }
        if(returns){
            boolean[] array1 = new boolean[8];
            array1[0] = array[7];
            array1[1] = array[6];
            array1[2] = array[5];
            array1[3] = array[4];
            array1[4] = array[3];
            array1[5] = array[2];
            array1[6] = array[1];
            array1[7] = array[0];
            array = array1;
        }

        return array;

    }


    public static Byte fromBooleanArray(boolean[] a) {
        byte b = 0;
        if (a[7])
            b += 1;
        if (a[6])
            b += 2;
        if (a[5])
            b += 4;
        if (a[4])
            b += 8;
        if (a[3])
            b += 16;
        if (a[2])
            b += 32;
        if (a[1])
            b += 64;
        if (a[0])
            b += 128;
        return b;
    }

    public static List<Boolean> toBoolArray(byte[] b) {
        List<Boolean> res = new ArrayList<>();
        for(int i=0;i<b.length;i++){
            boolean[] booleanArray = getBooleanArray(b[i], true);
            for(int j=0;j<booleanArray.length;j++){
                res.add(booleanArray[j]);
            }
        }
        return res;
    }

    public static boolean[] reverse(boolean[] b) {
        boolean[] res = new boolean[8];
        res[0] = b[7];
        res[1] = b[6];
        res[2] = b[5];
        res[3] = b[4];
        res[4] = b[3];
        res[5] = b[2];
        res[6] = b[1];
        res[7] = b[0];
        return res;
    }


    //bool array to byte array
    public static byte[] toByteArray(boolean[] b){
        Integer byteLength = CommonFunctions.exactDivision(b.length, 8);
        byte[] res = new byte[byteLength];
        Queue<Boolean> queue = new LinkedList<Boolean>();
        for(int i=0;i<b.length; i++){
            queue.add(b[i]);
        }

        //todo
        int z = 0;
        Integer boolLength =  queue.size();
        while (boolLength>0){
            boolean[] input = new boolean[8];

            if(boolLength>=8){
                for(int i = 0;i<8;i++){
                    input[i]= queue.poll();
                }


                Byte aByte = fromBooleanArray(reverse(input));
                res[z] = aByte;
                z++;
            }else {
                for(int i=0;i<boolLength;i++){
                    input[i]= queue.poll();
                }
                Byte aByte = fromBooleanArray(reverse(input));
                res[z] = aByte;
                z++;
            }
            boolLength = boolLength-8;
        }
        return res;
    }



//    public st atic List<Boolean> toBoolArray(byte[] b) throws UnsupportedEncodingException {
//        List<Boolean> res = new ArrayList<>();
//        for(int i=0;i<b.length;i++){
//            res.add(toBoolean(b[i]));
//        }
//        return res;
//    }

    public static List<Byte> toByteArray(byte[] b) {
        List<Byte> res = new ArrayList<>();
        for(int i=0;i<b.length;i++){
            res.add((b[i]));
        }
        return res;
    }

    public static List<Character> toCharArray(byte[] b) {
        List<Character> res = new ArrayList<>();
        for(int i=0;i<b.length;i++){
            res.add(toChar(b[i]));
        }
        return res;
    }

    //toWord
    /**
     * 默认：word => 有符号的整形
     * */
    public static List<Integer> toWordArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        int i=0;
        while ((i+2)<=b.length){
            res.add(
                    toInt(b[i],b[i+1])
            );
            i+=2;
        }
        return res;
    }

    //toDWord
    /**
     * 默认：dword => 有符号的整形
     * */
    public static List<Integer> toDWordArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        int i=0;
        while ((i+4)<=b.length){
            res.add(
                    toInt(b[i],b[i+1],b[i+2],b[i+3])
            );
            i+=4;
        }
        return res;
    }
    /**
     *
     * 4个字节byte[] 转成有符号的Double
     * 默认大端
     * */
    public static Float realbytesToFloat(byte[] bytes) {

        return Float.intBitsToFloat(toInt(bytes[0],bytes[1],bytes[2],bytes[3]));
    }
    public static byte[] realToBytes(Float f) {
        return invert(float2byte(f));
    }
    /**
     * 浮点转换为字节
     *
     * @param f
     * @return
     */
    private static byte[] float2byte(float f) {

        // 把float转换为byte[]
        int fbit = Float.floatToIntBits(f);

        byte[] b = new byte[4];
        for (int i = 0; i < 4; i++) {
            b[i] = (byte) (fbit >> (24 - i * 8));
        }

        // 翻转数组
        int len = b.length;
        // 建立一个与源数组元素类型相同的数组
        byte[] dest = new byte[len];
        // 为了防止修改源数组，将源数组拷贝一份副本
        System.arraycopy(b, 0, dest, 0, len);
        byte temp;
        // 将顺位第i个与倒数第i个交换
        for (int i = 0; i < len / 2; ++i) {
            temp = dest[i];
            dest[i] = dest[len - i - 1];
            dest[len - i - 1] = temp;
        }

        return dest;

    }

    //toUInt
    /**
     * USInt 无符号整形 1个字节  =》 Integer
     * */
    public static List<Integer> toUSIntArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        for(int i=0;i<b.length;i++){
            res.add(toUInt(b[i]));
        }
        return res;
    }
    /**
     * UInt 无符号整形 2个字节  =》 Integer
     * */
    public static List<Integer> toUIntArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        int i=0;
        while ((i+2)<=b.length){
            res.add(
                    toUInt(b[i],b[i+1])
            );
            i+=2;
        }
        return res;
    }
    /**
     * UDInt 无符号整形 4个字节  =》 Long
     * */
    public static List<Long> toUDIntArray(byte[] b) {
        List<Long> res = new ArrayList<>();
        int i=0;
        while ((i+4)<=b.length){
            res.add(
                    toUInt(b[i],b[i+1],b[i+2],b[i+3])
            );
            i+=4;
        }
        return res;
    }

    /**
     * SInt 无符号整形 1个字节  =》 Integer
     * */
    public static List<Integer> toSIntArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        for(int i=0;i<b.length;i++){
            res.add(toInt(b[i]));
        }
        return res;
    }
    /**
     * Int 无符号整形 2个字节  =》 Integer
     * */
    public static List<Integer> toIntArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        int i=0;
        while ((i+2)<=b.length){
            res.add(
                    toInt(b[i],b[i+1])
            );
            i+=2;
        }
        return res;
    }
    /**
     * DInt 无符号整形 4个字节  =》 Integer
     * */
    public static List<Integer> toDIntArray(byte[] b) {
        List<Integer> res = new ArrayList<>();
        int i=0;
        while ((i+4)<=b.length){
            res.add(
                    toInt(b[i],b[i+1],b[i+2],b[i+3])
            );
            i+=4;
        }
        return res;
    }

    /**
     * sint(1个字节)  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] sintToBytes(Integer i){
        byte[] res = new byte[1];
        res[0] = Byte.valueOf(i.toString());
        return res;
    }
    /**
     * sintArray(1个字节)  =》 byte[]
     *
     * */
    public static byte[] sintArrayToBytes(int[] sintArray) {
        byte[] res = new byte[sintArray.length];
        for(int i=0;i<sintArray.length;i++){
            int i1 = sintArray[i];
            res[i] = sintToBytes(i1)[0];
        }
        return res;
    }

    /**
     * int(2个字节)  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] intToBytes(Integer i) {
        Number shortNumber = Short.valueOf(i.toString());
        return ByteUtil.numberToBytes(shortNumber,ByteOrder.BIG_ENDIAN);
    }
    /**
     * intArray(2个字节)  =》 byte[]
     *
     * */
    public static byte[] intArrayToBytes(int[] intArray) {
        byte[] res = new byte[intArray.length*2];
        for(int i=0 ; i< intArray.length ; i++){
            byte[] bytes = intToBytes(intArray[i]);
            res[i*2] = bytes[0];
            res[i*2+1] = bytes[1];
        }
        return res;
    }

    /**
     * int(2个字节)  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] dintToBytes(Integer i) {
        Number intNumber = i;
        return ByteUtil.numberToBytes(intNumber,ByteOrder.BIG_ENDIAN);
    }
    /**
     * intArray(2个字节)  =》 byte[]
     *
     * */
    public static byte[] dintArrayToBytes(int[] dintArray) {
        byte[] res = new byte[dintArray.length*4];
        for(int i=0 ; i< dintArray.length ; i++){
            byte[] bytes = dintToBytes(dintArray[i]);
            res[i*4] = bytes[0];
            res[i*4+1] = bytes[1];
            res[i*4+2] = bytes[2];
            res[i*4+3] = bytes[3];

        }
        return res;
    }

    /**
     * sint(1个字节)  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] usintToBytes(Integer i){
        if(i<0){
            return null;
        }
        byte[] res = new byte[1];
        res[0] = Byte.valueOf(i.toString());
        return res;
    }
    /**
     * usintArrayToBytes  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] usintArrayToBytes(int[] usintArray) {
        byte[] res = new byte[usintArray.length];
        for(int i=0 ; i< usintArray.length ; i++){
            byte[] bytes = usintToBytes(usintArray[i]);
            if(bytes == null){
                return null;
            }
            res[i] = bytes[0];
        }
        return res;
    }


    /**
     * int(2个字节)  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] uintToBytes(Integer i) {
        if(i<0){
            return null;
        }
        Number shortNumber = Short.valueOf(i.toString());
        return ByteUtil.numberToBytes(shortNumber,ByteOrder.BIG_ENDIAN);
    }
    public static byte[] uintArrayToBytes(int[] uintArray) {
        byte[] res = new byte[uintArray.length*2];
        for(int i=0 ; i< uintArray.length ; i++){
            byte[] bytes = uintToBytes(uintArray[i]);
            if(bytes == null){
                return null;
            }
            res[i*2] = bytes[0];
            res[i*2+1] = bytes[1];
        }
        return res;
    }

    /**
     * int(2个字节)  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] udintToBytes(Integer i) {
        if(i<0){
            return null;
        }
        Number intNumber = i;
        return ByteUtil.numberToBytes(intNumber,ByteOrder.BIG_ENDIAN);
    }
    public static byte[] udintArrayToBytes(int[] udintArray) {
        byte[] res = new byte[udintArray.length*4];
        for(int i=0 ; i< udintArray.length ; i++){
            byte[] bytes = udintToBytes(udintArray[i]);
            if(bytes == null){
                return null;
            }
            res[i*4] = bytes[0];
            res[i*4+1] = bytes[1];
            res[i*4+2] = bytes[2];
            res[i*4+3] = bytes[3];
        }
        return res;
    }

    /**
     * boolean  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] boolToBytes(Boolean bool) {
        byte[] res = new byte[1];
        if(bool){
            res[0] = 1;
            return res;
        }else {
            res[0] = 0;
            return res;
        }
    }
    /**
     * booleanArray  =》 byte[]
     * */
    public static byte[] booleanArrayToBytes(boolean[] boolArray) {
        byte[] res = new byte[boolArray.length];
        for(int i=0 ; i<boolArray.length ; i++){
            boolean b = boolArray[i];
            if(b){
                res[i] = 1;
            }else {
                res[i] = 0;
            }
        }
        return res;
    }
    /**
     * charArray  =》 byte[]
     * */
    public static byte[] charArrayToBytes(char[] charArray) {
        byte[] res = new byte[charArray.length];
        for(int i=0 ; i< charArray.length ; i++){
            byte[] bytes = charToByte(charArray[i]);
            if(bytes[0] != 0){
                return null;
            }else {
                res[i] = bytes[1];
            }
        }
        return res;
    }





    /**
     * byte  =》 byte[]
     * 默认大端模式
     * */
    public static byte[] setbytes(Byte bytes) {
        byte[] res = new byte[1];
        res[0] = bytes;
        return res;
    }

    /**
     * word  =》 byte[]
     * 默认大端模式
     * tip: 目前只支持 带符号的十进制 ==》 word(2个字节)
     * */
    public static byte[] wordToBytes(Short word) {
        return ByteUtil.shortToBytes(word,ByteOrder.BIG_ENDIAN);
    }

    /**
     * wordArray  =》 byte[]
     * */
    public static byte[] wordArrayToBytes(short[] shortArray) {
        byte[] res = new byte[shortArray.length*2];
        for(int i=0 ; i< shortArray.length ; i++){
            byte[] bytes = wordToBytes(shortArray[i]);
            res[i*2] = bytes[0];
            res[i*2+1] = bytes[1];
        }
        return res;
    }

    /**
     * dword  =》 byte[]
     * 默认大端模式
     * tip: 目前只支持 带符号的十进制 ==》 word(4个字节)
     * */
    public static byte[] dwordToBytes(Integer dword) {
        return ByteUtil.intToBytes(dword,ByteOrder.BIG_ENDIAN);
    }

    /**
     * dwordArray  =》 byte[]
     * */
    public static byte[] dwordArrayToBytes(int[] intArray) {
        byte[] res = new byte[intArray.length*4];
        for(int i=0 ; i< intArray.length ; i++){
            byte[] bytes = dwordToBytes(intArray[i]);
            res[i*4] = bytes[0];
            res[i*4+1] = bytes[1];
            res[i*4+2] = bytes[2];
            res[i*4+3] = bytes[3];
        }
        return res;
    }

    /**
     * char(1字节)  =》 byte[]
     *
     * */
    public static byte[] charToBytes(Character c) {
        return String.valueOf(c).getBytes();
    }
    public static byte[] wcharToBytes(Character c) {
        return charToByte(c);
    }

    //如果plc中没有设置 strSize,那么 这个字符串变量的 最大长度默认是 254（字节就是-2 ）
    /**
     * 这个函数就用于 非str类型转bytes使用，string类型转bytes 用strToBytes(String s,Integer strSize) 这个方法。
     * */
    public static byte[] strToBytes(String s) {
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        byte[] res = new byte[bytes.length+2];
        res[0] = -2;
        res[1] = Integer.valueOf(bytes.length).byteValue();
        for(int i=0;i<bytes.length;i++){
           res[i+2] = bytes[i];
        }
        return res;
    }

    /**
     * desc
     * 入参：
     *      一般来说 plc中 string变量都是会设置 长度的，如 var1 = String[18],这里的strSize就是18
     * 返回：
     *      返回的字节流是包含2个头字节的，也就是 strSize+2 个长度的字节流。
     * */
    public static byte[] strToBytes(String s,Integer strSize) {
        if(s==null || (strSize<=0)){
            return null;
        }
        Integer allStrSize = strSize+2;
        byte[] bytes = s.getBytes(StandardCharsets.UTF_8);
        byte[] res = new byte[allStrSize];
        res[0] = strSize.byteValue();
        if(bytes.length>strSize){
            res[1] = strSize.byteValue();
            for(int i=0;i<strSize;i++){
                res[i+2] = bytes[i];
            }
        }else {
            res[1] = Integer.valueOf(bytes.length).byteValue();
            for(int i=0;i<bytes.length;i++){
                res[i+2] = bytes[i];
            }
        }
        return res;
    }

    public static void main(String[] args) throws UnsupportedEncodingException {
//        //stringArray <=> bytes[]
//        String[] src = new String[3];
//        src[0]= "1";
//        src[1]= "30110012105301117222";
//        src[2]= "3";
//        byte[] bytes = strArrayToBytes(src, 18);
//        String[] strings = toStrArray(bytes, 3, 18);
//        System.out.println(bytes);
//
//        //string <=> bytes[]
//        String s = "12323221";
//        byte[] bytes1 = strToBytes(s, 18);
//        String s1 = toStr(bytes1);
//        System.out.println(s1);

        boolean[] res = new boolean[22];
        res[0] = false;
        res[1] = true;
        res[2] = false;
        res[3] = true;
        res[4] = false;
        res[5] = true;
        res[6] = false;
        res[7] = false;

        res[8] = false;
        res[9] = true;
        res[10] = false;
        res[11] = false;
        res[12] = true;
        res[13] = false;
        res[14] = true;
        res[15] = false;


        res[16] = true;
        res[17] = false;
        res[18] = false;
        res[19] = false;
        res[20] = true;
        res[21] = false;

        byte[] bytes = toByteArray(res);

        Integer integer = CommonFunctions.exactDivision(7, 8);
        System.out.println();


    }

    /**
     * 参数
     *      desc    : 把字符串数组 ==> byte[]              [x,x,..,..,..,..,...   x,x,..,..,..,..,...   ...]
     *      array   : 就是需要被写入plc的内容,,  array.length 就是你要写入数组的长度。   注意array.length必须小于plc中设置的  “数组变量” length
     *      strSize : 就是数组中某个变量的长度  比如 String str = new String[18] 这里的18就是strSize,但是在博图偏移量地址上可以发现它已经把你+2了,就是你设置最大string长度18  但偏移量=18+2=20;;
     *                而上面的array.length是 List<String> list = new ArrayList<>; 的list.length
     * 返回
     *      byte[]  就是整个数组字符串的  总的字节流
     *
     * */
    public static byte[] strArrayToBytes(String[] array,Integer strSize) {
        //str0 [18, 17, 51, 48, 49, 49, 48, 48, 49, 50, 49, 48, 53, 51, 48, 49, 49, 49, 53]
        //str1 [18, 18, 51, 48, 49, 49, 48, 48, 49, 50, 49, 48, 53, 51, 48, 49, 49, 49, 53, 49]
        //str2 [18, 18, 51, 48, 49, 49, 48, 48, 49, 50, 49, 48, 53, 51, 48, 49, 49, 49, 53, 50]

        //list [18, 17, 51, 48, 49, 49, 48, 48, 49, 50, 49, 48, 53, 51, 48, 49, 49, 49, 53, 0, 18, 18, 51, 48, 49, 49, 48, 48, 49, 50, 49, 48, 53, 51, 48, 49, 49, 49, 53, 49, 18, 18, 51, 48, 49, 49, 48, 48, 49, 50, 49, 48, 53, 51, 48, 49, 49, 49, 53, 50]
        //strSize+2 以后就是 实际plc 变量的字节数了。
        Integer allStrSize = strSize+2;
        byte[] res = new byte[array.length*allStrSize];
        for(int i=0;i<array.length;i++){
            byte[] bytes = strToBytes(array[i],strSize);
            System.arraycopy(bytes,0,res,i*allStrSize,bytes.length);
        }
        return res;
    }



    //for private

    private static byte[] charToByte(char c) {
        byte[] b = new byte[2];
        b[0] = (byte) ((c & 0xFF00) >> 8);
        b[1] = (byte) (c & 0xFF);
        return b;
    }
    private static char byteToChar(byte[] b) {
        char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
        return c;
    }
    private static char byteToChar(byte b) {
        char c = (char) (b & 0xFF);
        return c;
    }


}
